package jpb.module.service.organize;

import jpb.module.dao.department.DepartmentDao;
import jpb.module.dao.follow.OrganizeFollowDao;
import jpb.module.dao.manager.ManagerOrgDao;
import jpb.module.dao.member.MemberDao;
import jpb.module.dao.organize.OrganizeDao;
import jpb.module.dao.organize.OrganizeMemDao;
import jpb.module.dao.profession.ProfessionDao;
import jpb.module.daomain.member.Member;
import jpb.module.daomain.member.OrgMem;
import jpb.module.daomain.organize.Organize;
import jpb.module.daomain.profession.Profession;
import jpb.util.DateFormatUtil;
import jpb.util.Identities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by haol on 2017/4/1.
 */
@Service
public class OrganizeService {
        @Autowired
        private OrganizeDao organizeDao;
        @Autowired
        private OrganizeMemDao organizeMemDao;
        @Autowired
        private ManagerOrgDao managerOrgDao;
        @Autowired
        private DepartmentDao departmentDao;
        @Autowired
        private ProfessionDao professionDao;
        @Autowired
        private MemberDao memberDao;
        @Autowired
        private OrganizeFollowDao organizeFollowDao;



        public void add(Organize organize, String mid) {
                String oid = Identities.uuid();
                organize.setId(oid);
                organize.setCreateTime(DateFormatUtil.getYMdHm());
                managerOrgDao.add(mid, oid, 1);
                organizeDao.add(organize);
        }

        public void delete(String id) {
                Organize organize = organizeDao.get(id);
                if (organize.getType() == 2) {
                        organizeDao.clearFollow(id);//清空关注本组织地成员
                        this.deleteMem(id);
                }
                organizeMemDao.delete(id);//删除组织先的所有成员关系
                managerOrgDao.delete(id);//删除本组织的所有管理员和创始人关系
                departmentDao.delete(id);//删除本组织下的所有部门
                professionDao.delete(id);//删除本组织下的所有职称
                organizeDao.delete(id);//删除组织本身
        }


        public void complete(Organize organize) {
                organizeDao.complete(organize);
                organizeDao.updateTime(organize.getId(), new Date().toString());
        }

        public Organize getByMark(String mark) {
                return organizeDao.getByMark(mark);
        }

        public Organize get(String id) {
                return organizeDao.get(id);
        }

        public void updateLogo(Organize organize) {
                organizeDao.updateLogo(organize);
                organizeDao.updateTime(organize.getId(), new Date().toString());
        }

        public List<Organize> getByMid(String mid) {
                return organizeDao.getByMid(mid);
        }

        public List<Organize> getByNum(String memberNum) {
                List<Organize> organizes = organizeDao.getByNum(memberNum);
                return organizes;
        }


        public void deleteMem(String id) {
                List<Member> members = memberDao.getByOid(id);
                for (Member member : members) {
                        memberDao.delete(member.getMemberNum());
                        memberDao.deleteMobile(member.getMemberNum());
                }
        }

        public void deleteOrgMem(String id) {
                organizeMemDao.delete(id);
        }


        public void deleteDep(String id) {
                departmentDao.delete(id);
        }

        public List<Member> getMem(String oid) {

                List<Member> members = memberDao.getByOid(oid);
                for (Member member : members) {
                        member.setOid(oid);
                        memberDao.addMobiles(member);
                }
                return members;
        }

//        public List<Member> getAllByDid(Integer did) {
//                List<Integer> depIds = departmentService.getAllSonId(did);
//                List<Member> members = new ArrayList<Member>();
//                for (Integer depId : depIds) {
//                        List<LoadMem> loadMems = memberDao.getByDid(depId);
//                        for (LoadMem loadMem : loadMems) {
//                                members.add(loadMem);
//                        }
//                }
//                return members;
//        }
        public List<Member> getByDid(Integer did) {
//                List<Member> members = new ArrayList<Member>();

                List<Member> members = memberDao.getByDid(did);
//                for (LoadMem loadMem : loadMems) {
//                        members.add(loadMem);
//                }

                return members;
        }

        public List<Organize> getPubOrgByNum(String num) {

                return organizeDao.getPubOrgByNum(num);
        }

        public List<Organize> getAllPubOrg() {
                return organizeDao.getPubOrg();
        }


        public void delMem(String oid, String memberNum) {
                organizeMemDao.delete(oid, memberNum);
        }

        public Integer getPidByNameAndOid(String oid, String proName) {
                Profession profession = professionDao.get(oid, proName);
                if (profession == null) {
                        return null;
                } else {
                        return profession.getId();
                }

        }

        public OrgMem transFrom(String memberNum, String oid) {
                OrgMem orgMem = new OrgMem();
                orgMem.setNum(memberNum);
                orgMem.setOid(oid);
                return orgMem;
        }

        public void updateOrgMem(String memberNum, String oid, Integer pid, Integer did) {
                this.updatePro(memberNum,oid,pid);
                this.updateDid(memberNum,oid,did);
        }

        public void updatePro(String memberNum, String oid, Integer pid) {
                OrgMem orgMem = this.transFrom(memberNum, oid);
                orgMem.setPid(pid);
                organizeMemDao.updatePid(orgMem);
        }

        public void updateDid(String memberNum, String oid, Integer did) {
                OrgMem orgMem = this.transFrom(memberNum, oid);
                orgMem.setDid(did);
                organizeMemDao.updateDid(orgMem);
        }

        public void addMem(String oid, int did, String memberNum, Integer pid) {
                OrgMem orgMem = new OrgMem();
                orgMem.setNum(memberNum);
                orgMem.setDid(did);
                orgMem.setPid(pid);
                orgMem.setOid(oid);
                if (!this.checkIsExist(oid, memberNum)) {
                        organizeMemDao.add(orgMem);
                } else {
                        this.updateOrgMem(memberNum, oid, pid, did);
                }
        }

        public boolean checkIsExist(String oid, String memberNum) {
                return organizeMemDao.queryByOidNum(oid, memberNum) != 0;
        }

        public boolean checkDepIsDeaf(Integer did) {
                return departmentDao.getSons(did).size() == 0;
        }

        public Integer getPidByName(String oid, String proName) {
                Profession profession = professionDao.get(oid, proName);
                if (profession == null) {
                        return null;
                } else {
                        return profession.getId();
                }
        }

        public List<Member> getFollowMem(String oid){
                return memberDao.getFollowByOid(oid);
        }





        public void add(String oid, String num) {
                organizeFollowDao.add(oid, num);
        }

        public void delete(String oid, String num) {
                organizeFollowDao.delete(oid, num);
        }

        public Integer get(String oid, String num){
                return organizeFollowDao.get(oid,num);
        }
}
